package com.test.ssm.controller;

import com.test.ssm.pojo.AjaxResult;
import com.test.ssm.pojo.TableData;
import com.test.ssm.pojo.User;
import com.test.ssm.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author 徒有琴
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping(params = "act=table")
    public TableData list(String name, Integer page, Integer limit) {
        return userService.getUserList(name, page, limit);
    }

    @RequestMapping(params = "act=edit")
    public AjaxResult edit(User user) {
        if (user.getId() == null) {
            userService.addUser(user);
        } else {
            userService.updateUser(user);
        }
        return new AjaxResult(true, null);
    }

    @RequestMapping(params = "act=info")
    public User info(Integer id) {
        return userService.getUserById(id);
    }

    @RequestMapping(params = "act=delete")
    public AjaxResult delete(Integer[] ids) {
        userService.deleteUsers(ids);
        return new AjaxResult(true, null);
    }
}
